<?php

include_once LIBRARIES_TOOLS_XTEMPLATE;
include_once LIBRARIES_ENTITIES_DATABASE;
include_once LIBRARIES_ENTITIES_ARTICLES;
include_once LIBRARIES_TOOLS_UTILITY;

class BlockHighLightArticleController {

    public function invoke() {
        $dir = dirname(__FILE__);

        // tải danh sách bài viết
        $rows = $this->getHighLightList();

        // tổng số tin 
        $amountOfNews = count($rows);


        if (count($rows) > 0) {

            $xtemplate = new XTemplate($dir . '/views/highlight_article.html');

            $amountToView = 4;

            if ($amountOfNews < $amountToView) {
                $amountToView = $amountOfNews;
            }

            for ($i = 0; $i < $amountToView; $i++) {
                if ($i == 0) {
                    $xtemplate->assign('FIRST_ARTICLE_ID', $rows[$i]->getID());
                    $xtemplate->assign('FIRST_ARTICLE_IMAGE', $rows[$i]->getImage());
                    $xtemplate->assign('FIRST_ARTICLE_ALIAS', $rows[$i]->getAlias());
                    $xtemplate->assign('FIRST_ARTICLE_TITLE', $rows[$i]->getTitle());
                    $xtemplate->assign('FIRST_ARTICLE_DESCRIPTION', $rows[$i]->getDescription());
                    $xtemplate->assign('FIRST_ARTICLE_DATE', Utility::formatDateTimeToView($rows[$i]->getModifiedDate()));
                    $xtemplate->parse("main.first_article");
                } else {
                    $xtemplate->assign('ARTICLE_ID', $rows[$i]->getID());
                    $xtemplate->assign('ARTICLE_IMAGE', $rows[$i]->getImage());
                    $xtemplate->assign('ARTICLE_ALIAS', $rows[$i]->getAlias());
                    $xtemplate->assign('ARTICLE_TITLE', $rows[$i]->getTitle());
                    $xtemplate->assign('ARTICLE_DESCRIPTION', $rows[$i]->getDescription());
                    $xtemplate->assign('ARTICLE_DATE', Utility::formatDateTimeToView($rows[$i]->getModifiedDate()));
                    $xtemplate->parse("main.article");
                }
            }

            $xtemplate->parse("main");
            $xtemplate->out("main");
        }
    }

    // get highlight article list
    private function getHighLightList() {
        $db = new Database();
        try {
            $article = new Articles();
            $rows = $article->getHighLightList($db);
            $db->submitChanges();
            return $rows;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}

?>
